package com.redmoon.oa.flow.macroctl;

import cn.js.fan.util.ParamUtil;
import cn.js.fan.util.StrUtil;
import com.cloudweb.oa.utils.ConstUtil;
import com.cloudweb.oa.utils.SpringUtil;
import com.redmoon.oa.flow.FormDb;
import com.redmoon.oa.flow.FormField;
import com.redmoon.oa.visual.FormDAO;
import javax.servlet.http.HttpServletRequest;

public class PerformanceScoreCtl extends AbstractMacroCtl {

    @Override
    public String convertToHTMLCtl(HttpServletRequest request, FormField ff) {
        String strReadonly = ff.isReadonly() ? "readonly" : "";
        String str = "";

        if (iFormDAO != null) {
            String params = ff.getDescription();
            // 引用的模板库指库项
            long quoteId = iFormDAO.getCwsQuoteId();

            // 取模板库的记录
            FormDb fdTempl = new FormDb();
            fdTempl = fdTempl.getFormDb(ConstUtil.FORM_KPI_TEMPLATE_DTL);
            FormDAO daoTempl = new FormDAO();
            daoTempl = daoTempl.getFormDAO(quoteId, fdTempl);

            // 取指标库的记录
            FormDb fdLib = new FormDb();
            fdLib = fdLib.getFormDb(ConstUtil.FORM_KPI_LIBRARY);
            FormDAO daoLib = new FormDAO();
            daoLib = daoLib.getFormDAO(daoTempl.getCwsQuoteId(), fdLib);

            // 取指标类型
            String scoreMethod = daoLib.getFieldValue("score_method");
            if ("1".equals(scoreMethod)) {
                String formulaCode = daoLib.getFieldValue("formula");
                // String params = daoLib.getFieldValue("params");
                    /*FormulaUtil formulaUtil = new FormulaUtil();
                    Formula formula = formulaUtil.getFormula(formulaCode);*/
                String cwsFormName = ParamUtil.get(request, "cwsFormName");

                str = "<input id='" + ff.getName() + "' name='" + ff.getName() + "' value='' style='width:" + ff.getCssWidth() + "' readonly />";
                if (ff.isEditable()) {
                    str += "<script>\n";
                    str += "initPerformanceScoreCtl('" + cwsFormName + "', '" + ff.getName() + "', '" + formulaCode + "', '" + params.replaceAll(",", ";") + "');\n";
                    str += "onPerformanceScoreCtlRelateFieldChange('" + cwsFormName + "', '" + ff.getName() + "', '" + formulaCode + "', '" + params.replaceAll(",", ";") + "');\n";
                    str += "</script>\n";
                }
                return str;
            }
        }
        str = "<input id='" + ff.getName() + "' name='" + ff.getName() + "' value='" + StrUtil.getNullStr(ff.getValue()) + "' style='width:" + ff.getCssWidth() + "' " + strReadonly + " />";
        return str;
    }

    @Override
    public String getControlType() {
        return FormField.TYPE_TEXTFIELD;
    }

    @Override
    public String getControlOptions(String userName, FormField ff) {
        return "";
    }

    @Override
    public String getControlValue(String userName, FormField ff) {
        return StrUtil.getNullStr(ff.getValue());
    }

    @Override
    public String getControlText(String userName, FormField ff) {
        return StrUtil.getNullStr(ff.getValue());
    }
}
